
$(()=>{
    //顶部的下拉菜单
    $(".nav_menu").find(".link-holder").on("mouseenter",_.debounce(function(){
        $(this).parents(".nav_menu").find(".menuaa").slideDown(100)
    },200))
    $(".nav_menu").find(".link-holder").on("mouseleave",function(){
        $(this).parents(".nav_menu").find(".menuaa").hide()
    })
    $('.goindex').on('click',function(){
        location.href ='index.html' 
    })
    //滚动
    $(window).on("scroll",function(){
        let topH = $(".top_box").height();
        let topH1= $(".main_c").height()+topH;
        let scrollTop =$(document).scrollTop();
        if(scrollTop>topH){
            $(".mian-headerbox").css("position","fixed")
            $(".mian-headerbox").css("top",0)
        }else{
            $(".mian-headerbox").css("position","static")
        }
        if(scrollTop>topH1){
            $(".h3-nav-list").addClass("fixed")
        }else{
            $(".h3-nav-list").removeClass("fixed")
        }
    })
    //回到顶部
    $(window).on("scroll",function(){
        let scrollTop = $(window).scrollTop();
        if(scrollTop>100){
            $(".backTop").show()
        }else{
            $(".backTop").hide();
        }
        
    })
    // 随机验证码
    function random_ma(){
        let str = "";
        for(let i=0;i<4;i++){
            if(Math.random()>0.5){
                str+=parseInt(Math.random()*10)
            }else if(Math.random()>0.5){
                str+=String.fromCharCode(parseInt(Math.random()*26)+97)
            }else{
                str+=String.fromCharCode(parseInt(Math.random()*26)+65)
            }
        }
        return str
    }
    //随机颜色
    function randcolor(){
        let strcolor='#'
        for(let i=0;i<6;i++){
            strcolor+=parseInt(Math.random()*6).toString(16)
        }
        return strcolor
    }
    $('.random-ma').html(random_ma()).css("color",randcolor())
    $('.backTop').on("click",function(){
        $('html,body').animate({scrollTop:0})
    })
    //自定义规则
    //$.validator.addMethod('规则名',函数)返回true和false
    $.validator.addMethod("checkName",(value,element,param)=>{
        return /^[a-z]\w+/ig.test(value);
    })
    $('form').validate({
        onkeyup:function(element) { $(element).valid(); },
        //rules 规则
        rules:{
            name:{
                required:true,//用户名必填
                checkName:true,//自定义的规则
                rangelength:[6,18],//长度6-18
                remote:'/api/checkName'
            },
            phone:{
                rangelength: [11,11],
                digits: true
            },
            pwd:{
                required: true,
                rangelength: [6, 18]
            },
            email:{
                required:true,
                email:true
            }
        },
        messages:{
            name:{
                required:'用户名必填',
                checkName:"用户名不合法",
                rangelength:'用户名长度必须{0}-{1}',
                remote: "该用户名已注册!"
            },
            phone:{
                rangelength: "手机号必须11位",
                digits: "只能是数字"
            },
            pwd:{
                required:'密码必填',
                rangelength: '长度必须{0}-{1}'
            },
            email:{
                required:'邮箱必填',
                email: "邮箱不合法" // 	email规则
            }
        },
        //等价于 onsubmit
        submitHandler(){
            console.log(111);
            // ajax...
            $('#reg').prop('disabled',true);

            let index = layer.load(1,{
                shade:[0.1,"#fff"]//0.1透明度的白色背景
            });
            $.ajax({
                url:"/api/reg",
                type:'post',
                data:$('form').serialize(),//网页表单里的所有信息
            }).then(function (res) {
                $('#reg').prop('disabled',false);
                layer.close(index);
                if(res && res.code == 200){
                    layer.confirm('恭喜,注册成功!,是否立即去登录',{
                        btn:['确定','取消']
                    },function(tmpIndex){
                        //由于点了确定,不会关闭确认框,需要
                        //手动关闭
                        layer.close(tmpIndex);

                        setTimeout(function(){
                            location.href = 'login.html';
                        },500);
                    },function(){
                        //取消  会自动关闭窗口
                    })
                }
            })
            return false;
        }
    })
})